﻿using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Presentation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

#nullable enable

namespace PPTHelper.BLL.TimingRoot
{
    /// <summary>
    /// 时序树的根其实也是一个并行时间容器
    /// </summary>
    public class TimingRootContainer : ParallelContainer
    {
        /// <summary>
        /// 如果幻灯片中有常规的动画，则时序根一定会有一个主序列。
        /// </summary>
        public SequenceContainer MainSequence
        {
            get
            {
                foreach (var timeNode in base.TimeNodeList)
                {
                    if (timeNode is SequenceContainer seq)
                    {
                        if (seq.CommonTimeNode.NodeType == TimeNodeValues.MainSequence)
                        {
                            return seq;
                        }
                    }
                }
                return null;
            }
        }

        ///// <summary>
        ///// 如果幻灯片有触发器动画，则时序根下一定会有交互序列。
        ///// </summary>
        //public IEnumerable<TimeNodeContainer> InteractiveSequence
        //{
        //    get
        //    {
        //        return  base.TimeNodeList.Where(i => i.CommonTimeNode.NodeType == TimeNodeValues.InteractiveSequence);
        //    }
        //}

        public TimingRootContainer()
        {
            Parent = null;
        }

        public override void Run(TriggerInfo triggerInfo)
        {
            Console.WriteLine("执行TimingRoot容器。");

            if (triggerInfo.TriggerType == "Click")
            {
                foreach (var timeNode in TimeNodeList)
                {
                    if (timeNode is SequenceContainer seq)
                    {
                        // 如果时交互型的节点
                        if (seq.CommonTimeNode.NodeType == TimeNodeValues.InteractiveSequence)
                        {
                            seq.Run(triggerInfo);
                        }
                    }
                }
            }
            else
            if (triggerInfo.TriggerType == "OnBegain")
            {
                var mainSeq = TimeNodeList.Where(i => i is MainSequenceContainer).FirstOrDefault();
            }


           
        }

        /// <summary>
        /// 容器启动时需要初始化一下，设置一些预设值
        /// </summary>
        public void StartInit()
        {
            Console.WriteLine("StartInit..."); ; ; ;

            SetTimeNodePresetValue(this, this.CommonTimeNode?.PresetClass);

        }


        /// <summary>
        /// 设置时间节点预设值
        /// </summary>
        /// <param name="timeNodeContainer"></param>
        /// <param name="presetClass"></param>
        private void SetTimeNodePresetValue(TimeNodeContainer timeNodeContainer, EnumValue<TimeNodePresetClassValues>? presetClass)
        {
            if (timeNodeContainer == null)
                return;

            //// 如果时预设进入动画，则关联的UIElement的相应属性要进行初始化

            foreach (var timeNode in timeNodeContainer.TimeNodeList)
            {
                if (timeNode is TimeNodeContainer tnc)
                {
                    var subPresetClass = tnc.CommonTimeNode?.PresetClass;

                    if (subPresetClass != null)
                    {
                        SetTimeNodePresetValue(tnc, subPresetClass);
                    }
                    else
                    {
                        SetTimeNodePresetValue(tnc, presetClass);
                    }
                }
                else
                if (timeNode is ActionContainer actionNdoe) // 操作类型的节点才有CommonBehavior属性，CommonBehavior也是一个容器
                {
                    var targetElement = actionNdoe.CommonBehavior?.TargetElement;

                    if (targetElement?.ShapeTarget != null && presetClass == TimeNodePresetClassValues.Entrance)
                    {
                        Console.WriteLine("设置了一个System.Windows.Visibility.Collapsed的预设值，ID=" + targetElement?.ShapeTarget.ShapeId);

                        // 进入类型的动画都隐藏起来
                        var elementInfo = ElementInfos.Where(i => i.Id == uint.Parse(targetElement?.ShapeTarget.ShapeId)).FirstOrDefault();
                        if (elementInfo != null)
                            elementInfo.UIElement.Visibility = System.Windows.Visibility.Collapsed;
                    }

                    SetTimeNodePresetValue(actionNdoe.CommonBehavior, presetClass);

                }

            }
        }

         
    }
}
